import { fileURLToPath, URL } from 'node:url'
import type { UserConfig, ConfigEnv } from 'vite'
import { loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite';
import NutUIResolver from '@nutui/auto-import-resolver';
import vueJsx from '@vitejs/plugin-vue-jsx'
import { createProxy } from './proxy'
import path from "path";
import basicSsl from '@vitejs/plugin-basic-ssl'
// UserConfig
// https://vitejs.dev/config/
export default ({ mode }: ConfigEnv): UserConfig | any => {
  const root = path.resolve(__dirname)
  const env = loadEnv(mode, root)
  const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = env
  return {
    plugins: [
      basicSsl(),
      vue(),
      vueJsx(),
      // 开启 unplugin 插件，自动引入 NutUI 组件
      Components({
        resolvers: [NutUIResolver() as []],
      }),
    ],
    base: VITE_PUBLIC_PATH,
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@nutui/nutui/dist/styles/variables.scss";`
        }
      },
    },
    server: {
      hmr: true, // 热更新
      host: true,
      https: true,
      open: true, // 浏览器自动打开
      port: Number(VITE_PORT),
      proxy: createProxy(VITE_PROXY),
    },
  }
}
